import rclpy
from rclpy.node import Node
form std_msgs.msg import String

class PublisherNode(Node)
    def __init__(self, name):
        super.__init__(name)
        self.pub = self.create_publisher(String, 'topic', 10) # 创建发布者
        self.timer = self.create_timer(1.0, self.timer_callback) # 创建定时器，1秒发布一次消息

    def timer_callback(self): # 回调函数
        msg = String()
        msg.data = "Hello, ROS 2!"
        self.get_logger().info('Publishing: "%s"' % msg.data) # 打印日志
    
def main(args=None):
    rclpy.init(args=args)
    node = PublisherNode('Hello Ros2_pub')
    rclpy.spin(node) # 保持节点运行
    node.destroy_node()
    rclpy.shutdown()
